//
//  MBSpaceGradeHeaderView.m
//  MiBaoTu
//
//  Created by admin on 16/9/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceGradeHeaderView.h"

@interface MBSpaceGradeHeaderView () {
    UIImageView                     *_userImgView;
    UILabel                         *_nickNameLb;
    UILabel                         *_gradeLb;
    UIProgressView                  *_progressView;
    UILabel                         *_myGradeLb;
    UILabel                         *_myGradeDetailLb;
}

@end
@implementation MBSpaceGradeHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImgView.backgroundColor = [UIColor clearColor];
    _userImgView.layer.cornerRadius = 60*PERCEN;
    _userImgView.layer.masksToBounds = YES;
    [self addSubview:_userImgView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:14.f];
    [self addSubview:_nickNameLb];
    
    //等级
    _gradeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _gradeLb.backgroundColor = [UIColor clearColor];
    _gradeLb.textAlignment = NSTextAlignmentLeft;
    _gradeLb.textColor = TINK_COLOR;
    _gradeLb.font = _nickNameLb.font;
    [self addSubview:_gradeLb];
    
    _progressView = [[UIProgressView alloc]initWithFrame:CGRectZero];
    _progressView.progressViewStyle = UIProgressViewStyleDefault;
    _progressView.progressTintColor = TINK_COLOR;
    _progressView.trackTintColor = TRACK_COLOR;
    _progressView.progress = 0;
    [self addSubview:_progressView];
    
    //我的等级
    _myGradeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _myGradeLb.backgroundColor = [UIColor clearColor];
    _myGradeLb.textAlignment = NSTextAlignmentLeft;
    _myGradeLb.textColor = UIColorFromHex(0x999999);
    _myGradeLb.font = [UIFont systemFontOfSize:12.f];
    [self addSubview:_myGradeLb];
    
    //等级差值
    _myGradeDetailLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _myGradeDetailLb.backgroundColor = [UIColor clearColor];
    _myGradeDetailLb.textAlignment = NSTextAlignmentLeft;
    _myGradeDetailLb.textColor = _myGradeLb.textColor;
    _myGradeDetailLb.font = _myGradeLb.font;
    [self addSubview:_myGradeDetailLb];

}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //value
    UserModel *model = [UserManager standerDefult].userModel;
    if (!_dataDic) {
        //请求失败
        [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,model.FaceImg]] placeholderImage:[UIImage imageNamed:@"touxiang_attention"]];
        _nickNameLb.text = [NSString stringWithFormat:@"%@",model.NickName];
        _gradeLb.text = [NSString stringWithFormat: @"Lv.1"];
        _myGradeLb.text = [NSString stringWithFormat:@"我的积分0"];
        _myGradeDetailLb.text = [NSString stringWithFormat:@"距离Lv.2还差100积分"];
        [_progressView setProgress:0 animated:YES];
    } else {
        NSString *faceImg = model.FaceImg;
        if ([faceImg isKindOfClass:[NSNull class]]) {
            _userImgView.image = [UIImage imageNamed:@"touxiang_attention"];
        } else {
            [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,model.FaceImg]] placeholderImage:[UIImage imageNamed:@"touxiang_attention"]];
        }
        
        _nickNameLb.text = [NSString stringWithFormat:@"%@",model.NickName];
        _gradeLb.text = [NSString stringWithFormat: @"Lv.%@",[_dataDic objectForKey:@"UserGrade"]];
        _myGradeLb.text = [NSString stringWithFormat:@"我的积分%@",[_dataDic objectForKey:@"UserScore"]];
        _myGradeDetailLb.text = [NSString stringWithFormat:@"距离Lv.%d还差%d积分",[[_dataDic objectForKey:@"UserGrade"] intValue]+1,[[_dataDic objectForKey:@"NextScore"] intValue]-[[_dataDic objectForKey:@"UserScore"] intValue]];
        float progress = 1.00 * [[_dataDic objectForKey:@"UserScore"]intValue] / [[_dataDic objectForKey:@"NextScore"] intValue];
        [_progressView setProgress:progress animated:YES];
    }
    
    //frame
    _userImgView.frame =  CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 20*PERCEN, 120*PERCEN, 120*PERCEN);
    
    float nickWidth = [_nickNameLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_nickNameLb.font} context:NULL].size.width+4;
    _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, self.height/2-20-10*PERCEN, nickWidth, 20);
    _gradeLb.frame = CGRectMake(_nickNameLb.right+10*PERCEN, _nickNameLb.top, 80, 20);
    
    _progressView.frame = CGRectMake(_nickNameLb.left, self.height/2.-1, (ISIPAD?SCREEN_WIDTH-_nickNameLb.left-460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):500*PERCEN), 2);
    
    float gradeWidth = [_myGradeLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_myGradeLb.font} context:NULL].size.width+4;
    _myGradeLb.frame = CGRectMake(_nickNameLb.left, _progressView.bottom+10*PERCEN, gradeWidth, 20);
    
    _myGradeDetailLb.frame = CGRectMake(_myGradeLb.right+10*PERCEN, _myGradeLb.top, 300*PERCEN, 20);
}

@end
